package cs271.examples.recursion;

import java.util.ArrayList;
import java.util.List;

public class Lists {
	
	public static int count(List<Integer> list) {
		if (list.size()==0)	
			return 0;
		else
			return 1+count(rest(list));
	}
	
	public static int sum(List<Integer> list) {
		if (list.size()==0)	
			return 0;
		else
			return first(list)+sum(rest(list));
	}
	
	public static int multiply(List<Integer> list) {
		if (list.size()==0)	
			return 1;
		else
			return first(list)*multiply(rest(list));
	}
	
	public static int max(List<Integer> list) {
		return max_sub(first(list), rest(list));
	}
	
	public static int max_sub(int max, List<Integer>list) {
		if (list.size()==0){
			return max;
		} else {
			int newmax = max_sub(first(list), rest(list));
			if( max > newmax){
				return max;
			}else {
				return newmax;
			}
		}
	}
	
	public static int min(List<Integer> list) {
		return min_sub(first(list), rest(list));
	}
	
	public static int min_sub(int min, List<Integer>list) {
		if (list.size()==0){
			return min;
		} else {
			int newmin = min_sub(first(list), rest(list));
			if( min < newmin){
				return min;
			}else {
				return newmin;
			}
		}
	}
	
	public static int first(List<Integer> list) {
		return list.get(0);
	}
	
	public static List<Integer> rest(List<Integer> list) {
		return list.subList(1, list.size());
	}
	
	public static void main(String[] args) {
		List<Integer> list = new ArrayList<Integer>();
		list.add(1); list.add(2); list.add(3); list.add(4);
		
		System.out.println(first(list));
		System.out.println(rest(list));
		
		System.out.println("Count: " + count(list));
		System.out.println("Sum: " + sum(list));
		System.out.println("Multiply: " + multiply(list));
		System.out.println("Max: " + max(list));
		System.out.println("Min: " + min(list));
	}
}